import {
  Controller,
  Inject,
  Post,
  Get,
  Query,
  Files,
} from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { JwtPassportMiddleware } from '../middleware/jwt.middleware';
import { UploadService } from '../service/upload.service';
import { JwtService } from '@midwayjs/jwt';

@Controller()
export class FileController {
  @Inject()
  ctx: Context;

  @Inject()
  jwt: JwtService;

  @Inject()
  userService: UserService;

  @Inject()
  uploadService: UploadService;

  @Get('/fileDownload')
  async fileDangerousDownload(@Query('filename') filename: string) {
    this.ctx.set('Content-Disposition', `attachment; filename=${filename}`);
    return this.uploadService.getResource('vulnerable', filename, true);
  }

  @Post('/fileUpload', { middleware: [JwtPassportMiddleware] })
  async fileDangerousUpload(@Files() files: any) {
    return await this.uploadService.uploadFileDangerously(files);
  }
}
